package com.ruoyi.system.aikeService;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.apiResult.ApiRe;
import com.ruoyi.common.utils.SpringContextUtil;
import com.ruoyi.system.apiService.ZhtcService;
import com.ruoyi.system.dto.UserDTO;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class ToZhtcApiService {
	public static final Logger logger = LoggerFactory.getLogger(ToZhtcApiService.class);
	private String key1 ="6c3wgf";
	private String secret1 ="gtj57pif0yu33f19drphm1lu0xy94w3x";
	private String url2 ="https://open.yidianting.xin/openydt/api/v2/";
	private String url3 ="https://open.yidianting.xin/openydt/api/v3/";
	private static final Map<String,Integer> cmdM = new HashMap();
	private static final Map<String,Integer> upcmd = new HashMap();
	@Autowired
	private HttpServletRequest request;
	@Autowired
	private ZhtcService zhtcService;
	@Autowired
	private SpringContextUtil springContextUtil;
	{
//        上报接口
		upcmd.put("reportCarIn",1);//进场上报
		upcmd.put("reportCarOut",1);//离场上报
//        upcmd.put("reportPreCarIn",1);//预进场上报
		upcmd.put("reportPreCarOut",1);//预离场上报
		upcmd.put("reportParkingFee",1);//临停缴费上报

		//-1不让访问，1可以调用不需要登录，2为需要登录，3
//        cmdM.put("opGate",-1);//开关闸
		cmdM.put("setPrestore",-1);//下发预存款
		cmdM.put("payParkFee",-1);//支付

//        cmdM.put("setPrestore",1);//设置预存款
		cmdM.put("getParkFee",1);//查费
		cmdM.put("scanChannelCodeInOut",2);//扫通道二维码自助进出场
		cmdM.put("opShowVoice",2);//语音播报
		cmdM.put("getChannelInfo",1);//云停车场通道信息
	}

	/**
	 *    内部程序调用接口
	 * @Author gongchengbing
	 * @Date  2020/8/22 0022 11:08
	 * @Param [cmd, params]
	 * @return java.lang.String
	 */
	public  String privateApi(String cmd, String params, Map addParam) {
		ApiRe re = new ApiRe();
		System.out.println("privateApi==========cmd===" + cmd+"====params=="+params);
		String date = DateUtil.format(new Date(),"yyyyMMddHHmmss");
		String sign = SecureUtil.md5(key1 +":" + date +":" + secret1);
		sign = sign.toLowerCase();
		String auth = Base64.encode(key1 +":" + date);
		String resultStr =
			HttpRequest.post(url2 + cmd +"?sign=" + sign)
				.header("Accept","application/json")
				.header("Content-Type","application/json;charset=utf-8")
				.header("Authorization", auth)
				.body(params)
				.execute().body();
		System.out.println("privateApi==========resultStr===" + resultStr);
		try {
			if (cmdM.get(cmd ) != null&&addParam.get("type")==null) {
				Class<?> clazz = Class.forName("com.ruoyi.system.aikeService.HomeZhtcApiService");
				Method method = clazz.getMethod(cmd,String.class,Map.class);
				Object obj1 = method.invoke(springContextUtil.getBean(clazz),resultStr,addParam);
				return obj1.toString();
			}
		}catch (Exception e){
			e.printStackTrace();
			return resultStr;
		}
		return resultStr;
	}

	public String greeting(@RequestParam(value ="name", defaultValue ="World") String name) {
		logger.info("======================测试hello world=======================");
		return"Hello"+name;
	}


}
